using System;
using System.IO;
using Kopernikus.Collections.Generic;

namespace Kopernikus.Parsing.Tests.TestUtility
{
    public class TestGrammarBase
    {
        public static IGrammar ParseGrammar(string grammarText)
        {
            GrammarBuilder grammarBuilder = new GrammarBuilder();

            TextReader reader = new StringReader(grammarText);
            string line;
            while (null != (line = reader.ReadLine()))
            {
                line = line.Trim();
                if (!string.IsNullOrEmpty(line))
                {
                    Production p = Production.Parse(line);
                    if (p != null)
                    {
                        p = grammarBuilder.Productions.Add(p);
                        if (grammarBuilder.StartProductions.Count == 0)
                        {
                            grammarBuilder.StartProductions.Add(p);
                        }
                    }
                }
            }
            return grammarBuilder.CreateGrammar();
        }

        public static Set<Symbol> MakeSymbolSet(params string[] symbols)
        {
            return
                new Set<Symbol>(Array.ConvertAll<string, Symbol>(symbols, delegate(string s) { return new Symbol(s); }));
        }
    }
}